﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CoursePack;

namespace WindowsFormsApplication2.Forms
{
    public partial class frmSemesterPicker : Form
    {
        private Student m_Student;

        public frmSemesterPicker(Student _student)
        {
            InitializeComponent();
            this.m_Student = _student;
        }
        
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (numericUpDown1.Value < 1 || numericUpDown1.Value > 4)
            {
                numericUpDown1.Value = 1;
            }
        }

        private void frmSemesterPicker_Load(object sender, EventArgs e)
        {
            #region Loading Courses
            ESession session = ESession.Winter;

            List<Course> Courses = new List<Course>();

            System.IO.StreamReader file = new System.IO.StreamReader(
                + this.m_Student.Degree.Department.Faculty.Id + "\\"
                + this.m_Student.Degree.Department.Id + "\\"
                + session.ToString()
                + "2012"
                + ".txt");

            this.treeView1.SelectedNode = null;
            file.ReadLine();
            file.ReadLine();
            file.ReadLine();
            string line;

            int j = 0;
            while ((line = file.ReadLine()) != null)
            {

                j++;
                string[] tab = line.Split('|');

                for (int i = 0; i < tab.Length; i++)
                {
                    tab[i] = tab[i].Trim();
                }


                Class Lecture = new Class(ClassType.Lecture, tab[3], tab[4], tab[5], tab[6], tab[7], tab[8]);
                Class Tutorial = null, Lab = null;

                if (tab[9] != "")
                    Tutorial = new Class(ClassType.Tutorial, tab[9], tab[10], tab[11], tab[12], tab[13]);

                if (tab[14] != "")
                    Lab = new Class(ClassType.Lab, tab[14], tab[15], tab[16], tab[17], tab[18]);

                Course course = new Course(tab[0], tab[1], tab[2], Lecture, Tutorial, Lab, tab[19], tab[20], tab[21], tab[22], CourseStatus.NotStarted);
                Courses.Add(course);
            }
            file.Close();

            TreeNode CourseNode = new TreeNode(Courses[0].ToString());


            TreeNode TutorialN, LabN, LectureN;

            this.treeView1.Nodes.Clear();

            foreach (Course course in Courses)
            {
                j++;
                string str1 = CourseNode.Text;
                string str2 = course.ToString();
                if (str1 != str2)
                {
                    CourseNode = new TreeNode(course.ToString());
                    this.treeView1.Nodes.Add(CourseNode);
                }
                
                LectureN = new TreeNode(course.Lecture.ToString());
                LectureN.Tag = course;
                
                CourseNode.Nodes.Add(LectureN);
               
               /*
                if (course.HasTutorial)
                {
                    TutorialN = new TreeNode(course.Tutorial.ToString());
                    TutorialN.Tag = course;

                    if (course.HasLab)
                    {
                        LabN = new TreeNode(course.Lab.ToString());
                        LabN.Tag = course;
                        TutorialN.Nodes.Add(LabN);
                        LectureN.Nodes.Add(TutorialN);


                    }
                    else
                    {
                        LectureN.Nodes.Add(TutorialN);
                    }
                }
                else if (course.HasLab)
                {
                    LabN = new TreeNode(course.Lab.ToString());
                    LabN.Tag = course;
                    LectureN.Nodes.Add(LabN);
                }
                 */
            }
            
            
            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode != null)
            {
                UserControls.CourseChoice courseChoice = new UserControls.CourseChoice(this.treeView1.SelectedNode);
                courseChoice.OnRemoveCourse += courseChoice_OnRemoveCourse;

                courseChoice.Size = new Size(this.flowLayoutPanel1.Width - 8, courseChoice.Size.Height);
                this.flowLayoutPanel1.Controls.Add(courseChoice);
            }
        }

        void courseChoice_OnRemoveCourse(object sender, EventArgs e)
        {
            ((UserControls.CourseChoice)sender).OnRemoveCourse -= courseChoice_OnRemoveCourse;

            this.flowLayoutPanel1.Controls.Remove((Control)sender);
        }

        private void vistaButton1_Click(object sender, EventArgs e)
        {
            this.flowLayoutPanel1.Controls.Clear();

        }

        private void vistaButton3_Click(object sender, EventArgs e)
        {/*
            Schedule schedule = new Schedule(new DateTime(), this.m_Student);

            foreach (UserControls.CourseChoice course in this.flowLayoutPanel1.Controls)
            {
                schedule.AddCoursesNode(course.Courses);    
            }

            schedule.Generate();

            */
        }
    }
}
